package com.hsnn.medstgmini.pro.plan.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.plan.dao.BasePriceadjplanDao;
import com.hsnn.medstgmini.pro.plan.model.BasePriceadjplan;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 调价计划 接口DAO实现类
 * @author 韩守松
 * @date 2015年6月17日
 */
@Repository
public class BasePriceadjplanDaoImpl extends BaseHibernateDAO implements BasePriceadjplanDao {
	
	@Autowired
	private HibernateSqlUtil dao;
	/**
	 * @category 查询调价计划数据
	 * @author 韩守松
	 * @date 2015年6月17日
	 * @param @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<BasePriceadjplan> getBasePriceadjplanList(Pagination page, BasePriceadjplan plan) {
		
		String createStartTime = page.getConditions().get("createStartTime");
		String createEndTime = page.getConditions().get("createEndTime");
		String executeStartTime = page.getConditions().get("executeStartTime");
		String executeEndTime = page.getConditions().get("executeEndTime");
		
		Criteria criteria = getSession().createCriteria(BasePriceadjplan.class);// 调价计划
		criteria.add(Restrictions.eq("status", (short) 1));

		if (plan != null) {
			String priceAdjPlanName = plan.getPriceAdjPlanName();
			if (StringUtils.isNotBlank(priceAdjPlanName)) {
				criteria.add(Restrictions.like("priceAdjPlanName", priceAdjPlanName, MatchMode.ANYWHERE));
			}
			Short executeCat = plan.getExecuteCat();
			if (executeCat != null) {
				criteria.add(Restrictions.eq("executeCat", executeCat));
			}
		}
		
		if (StringUtils.isNotBlank(createStartTime) && StringUtils.isNotBlank(createEndTime)) {
    	    criteria.add(Restrictions.ge("createDatetime", DateUtil.parseDateTime(createStartTime + " 00:00:00")));
    	    criteria.add(Restrictions.le("createDatetime", DateUtil.parseDateTime(createEndTime + " 23:59:59")));
		}
		
		if (StringUtils.isNotBlank(executeStartTime) && StringUtils.isNotBlank(executeEndTime)) {
    	    criteria.add(Restrictions.ge("executeDatetime", DateUtil.parseDateTime(executeStartTime + " 00:00:00")));
    	    criteria.add(Restrictions.le("executeDatetime", DateUtil.parseDateTime(executeEndTime + " 23:59:59")));
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.desc("executeDatetime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		List<BasePriceadjplan> list = criteria.list();
		page.setRows(list);

		return list;
	}
	
	/**
	 * 
	 * @category 获取已调价计划列表
	 * @auth wangbing
	 * @return
	 * @date 2015年6月23日
	 */
	@Override
	public Pagination getChangePlan(Pagination page) {
		String createStartTime = page.getConditions().get("createStartTime");
		String createEndTime = page.getConditions().get("createEndTime");
		String executeStartTime = page.getConditions().get("executeStartTime");
		String executeEndTime = page.getConditions().get("executeEndTime");
		String executeCat = page.getConditions().get("executeCat");
		String priceAdjPlanName = page.getConditions().get("priceAdjPlanName");
		String orgId = page.getConditions().get("orgId");
		StringBuffer sql = new StringBuffer()
		.append("	select a.price_adj_plan_id priceAdjPlanId,a.price_adj_plan_name priceAdjPlanName,a.price_adj_plan_cat priceAdjPlanCat,")
		.append("	a.execute_cat executeCat,a.create_datetime createDatetime,b.name createUser,")
		.append("	a.execute_datetime executeDatetime,a.price_adj_accord priceAdjAccord,")
		.append("	a.remark remark,a.status status,a.upd_datetime updDatetime,c.name updUser ")
		.append("	from base_priceadjplan a left join sys_user b on b.id = a.create_user left join sys_user c on c.id = a.upd_user ")
		.append("	where a.status = 1 and a.create_org_id = :orgId ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgId", orgId);
		if(StringUtils.isNotBlank(createStartTime)){
			sql.append(" and a.create_datetime >= :createStartTime ");
			params.put("createStartTime", createStartTime + " 00:00:00");
		}
		if(StringUtils.isNotBlank(createEndTime)){
			sql.append(" and a.create_datetime <= :createEndTime ");
			params.put("createEndTime", createEndTime + " 23:59:59");
		}
		if(StringUtils.isNotBlank(executeStartTime)){
			sql.append(" and a.execute_datetime >= :executeStartTime ");
			params.put("executeStartTime", executeStartTime + " 00:00:00");
		}
		if(StringUtils.isNotBlank(executeEndTime)){
			sql.append(" and a.execute_datetime <= :executeEndTime ");
			params.put("executeEndTime", executeEndTime + " 23:59:59");
		}
		if(StringUtils.isNotBlank(executeCat)){
			sql.append(" and a.execute_cat = :executeCat ");
			params.put("executeCat", executeCat);
		}
		if(StringUtils.isNotBlank(priceAdjPlanName)){
			sql.append(" and a.price_adj_plan_name like :priceAdjPlanName ");
			params.put("priceAdjPlanName", "%"+priceAdjPlanName+"%");
		}
		sql.append(" order by a.execute_datetime desc ");
		
		return dao.getPageMapList(sql.toString(), params, page);
	}

}
